import { defineConfig, splitVendorChunkPlugin } from 'vite'
import { resolve } from "path" 
import react from '@vitejs/plugin-react-swc'
import legacy from '@vitejs/plugin-legacy'
import postcssPxToViewport from 'postcss-px-to-viewport';
import browserslistToEsbuild from 'browserslist-to-esbuild';


// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  esbuild: {
    legalComments: 'none',
  },
  build: {  
    emptyOutDir: true,
    target: browserslistToEsbuild(),
    rollupOptions: {  
      output: {
        chunkFileNames: 'js/[name].[hash].js',
        entryFileNames: 'js/[name].[hash].js',
        assetFileNames: 'assets/[name].[hash].[ext]',
        manualChunks: {
          reactVendor: ['react', 'react-dom', 'react-router-dom'],
        },
      },
      input: {  
        main: resolve('index.html'), 
      }
    }  
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    },
    modules: {
      localsConvention: 'camelCase'
    },
    postcss: {
      plugins: [
        postcssPxToViewport({  
          unitToConvert: 'px', // 要转化的单位  
          viewportWidth: 750, // UI设计稿的宽度  
          unitPrecision: 6, // 转换后的精度,即小数点位数  
          propList: ['*'], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换  
          viewportUnit: 'vw', // 指定需要转换成的视窗单位,默认vw  
          selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名, 
          minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换 
          mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false 
          replace: true, // 是否转换后直接更换属性值 
          exclude: [/node_modules/, /h5/], // 设置忽略文件,用正则做目录名匹配 exclude: [], 
          landscape: false // 是否处理横屏情况   
        })
      ]
    }
  },
  resolve: {
    alias: {
      "@": resolve("/src"), //把 src 的别名设置为
    },
  },
  plugins: [
    react(),
    legacy({
      targets: ['defaults', 'not IE 11'],
    }),
    splitVendorChunkPlugin()
  ],
  server: {
    port: 3000,
    proxy: {}
  },
})
